<?php

class LogController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Registry::get('layout')->setLayout('duty');
        
        $activeResource=$this->getRequest()->getControllerName();
        
        // seting active resource
        Zend_Registry::set('resource',$activeResource);

        $this->view->headTitle()->append(Zend_Registry::get('locale')->_('Access log'));
        
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->view->flash_messages = $this->_flashMessenger->getMessages();
    }

    public function indexAction()
    {
        return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl('log/list'));
    }

    /**
     * Log listing action
     */
    public function listAction()
    {
        if (!Zend_Registry::get('user')->canView())
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        $key=(int) $this->_getParam('key');
        
        $logTbl=new Application_Model_LogDb();

        list($logs,$paginator)=$logTbl->getLogsPage($key,10);
        
        $this->view->logs=$logs;
        $this->view->paginator=$paginator;
    }
    
    /**
     * Log statistics action
     */
    public function statAction()
    {
        if (!Zend_Registry::get('user')->canView())
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }
        
        $logTbl=new Application_Model_LogDb();

        $uas=$logTbl->fetchTodayUA();
        $referers=$logTbl->fetchTodayReferers();
        $ips=$logTbl->fetchTodayIP();
        
        $this->view->uas=$uas;
        $this->view->referers=$referers;
        $this->view->ips=$ips;
    }
    
    /**
     * Log clear action
     */
    public function clearAction()
    {
        if (!Zend_Registry::get('user')->canManage())
        {
            return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl());
        }

        $logTbl=new Application_Model_LogDb();

        $logTbl->clear();
        
        $this->_helper->FlashMessenger(Zend_Registry::get('locale')->_('Access log cleared'));
     //   return $this->_helper->redirector('list');
        return $this->_response->setRedirect(Zend_Registry::get('view')->fixUrl('log/list'));
    }
}

